#from sqlalchemy import make_url
from flask_sqlalchemy import SQLAlchemy, make_url
from sqlalchemy import create_engine, MetaData, Table

engine = create_engine('sqlite:////tmp/test.db', convert_unicode=True)
metadata = MetaData(bind=engine)

db = SQLAlchemy()
Base = db.Model

def get_mysql_conn_url(config):
    mysql_conn_map = dict(
        dialect="mysql",
        driver="pymysql",
        host=config["MYSQL_HOST"],
        port=config["MYSQL_PORT"],
        database=config["MYSQL_DB"],
        user=config["MYSQL_USER"],
        password=config["MYSQL_PASSWORD"],
    )
    s = "{dialect}+{driver}://{user}:{password}\
@{host}:{port}/{database}?charset=utf8".format(**mysql_conn_map)
    url = make_url(s)
    print(s)
    return url


def init_app(app):
    url = get_mysql_conn_url(config=app.config)
    app.config["SQLALCHEMY_DATABASE_URI"] = url
    from . import models
    db.init_app(app)


